<template>
	<view class="mainpadding">
		<!-- 上传头像 -->
		<view class="touxiang" @click="uploadImg">
			<view class="shangchaun">
				<image :src="avatar" mode=""></image>
			</view>
			<image class="shext" src="../../static/image/system/shext.png" mode="" ></image>
		</view>
		<view class="flexleft margin_top youbianju">
			<view class="notitletext margin_right3">昵称</view>
			<input type="text" class="inputs mainpadding" placeholder="请输入昵称" v-model="nicheng">
		</view>
		<view class="flexleft margin_top youbianju">
			<view class="notitletext margin_right3">签名</view>
			<input type="text" class="inputs mainpadding" placeholder="请输入签名" v-model="qianming">
		</view>
		<!-- 底部 -->
		<view class="dibuboxs" @click="querenxg">
			<view class="dibubtn">确认修改</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				avatar: "",
				nicheng: "", //昵称
				qianming: "", //签名
			}
		},
		onShow() {
			this.mypage()
		},
		methods: {
			// 上传图片
			uploadImg() {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						console.log(res)
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], _this.upImg)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									console.log(res[i])
									_this.uploadImageService(res[i], _this.upImg)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			// 详情转载
			uploadImageService(url, upImg) {
				let _this = this;
				httpRequest.uploadFile('common/upload', url).then(res => {
					this.avatar = res.fullurl;
				}).catch(err => {});
			},
			//个人资料
			mypage() {
				httpRequest.request('user/index', 'POST', {}, false, false, true).then(res => {
					this.avatar = res.data.userinfo.avatar
					this.nicheng = res.data.userinfo.nickname
					this.qianming = res.data.userinfo.bio
				})
			},
			//修改资料
			querenxg() {
				httpRequest.request('user/profile', 'POST', {
					avatar: this.avatar,
					nickname: this.nicheng,
					bio: this.qianming
				}, false, false, true).then(res => {
					// 返回上一页
					if (res.code == 1) {
						httpRequest.toast("修改成功")
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.youbianju {
		margin-left: 30rpx;
	}

	.inputs {
		background: #F6F6F6;
		border-radius: 20rpx;
		width: 72%;
	}

	.touxiang {
		position: relative;
	}

	.shext {
		width: 40rpx;
		height: 36rpx;
		position: absolute;
		bottom: -6%;
		left: 47.5%;
	}

	.shangchaun {
		margin: auto;
		width: 153rpx;
		height: 154rpx;
		border-radius: 50% !important;

		image {
			border-radius: 50% !important;
			width: 100%;
			height: 100%;
		}
	}
</style>
